<template>
	<view class="orders" :class="orderList.length==0?'empty':''">
		<block v-if="orderList.length>0">
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item,index) in orderList" :key="index" :class="item.orderStatus==3?'qx':''"
					@tap.prevent="toBooking(item)">
					<view class="top">
						<view class="left">
							<image src="https://i.ringzle.com/file/20240314/b65d88259b794d42bfa2aa752c486ebe.png">
							</image>
							<text>餐饮美食</text>
						</view>
						<view class="right">
							<block v-if="item.orderStatus==0&&item.djsTime">
								<uCountDown @finish="finish(item)" :djsTime="item.djsTime" />
							</block>
							<block v-else>
								<text
									:class="oscConfig[item.orderStatus]||'textColor999'">{{FishJialeOcfg[item.orderStatus]}}</text>
							</block>
						</view>
					</view>
					<view class="content">
						<view class="title">
							<text>{{item.merchantName}}</text>
							<text>￥{{item.orderRealityPay}}</text>
						</view>
						<view class="date">

						</view>
						<view class="room_price">
							<text>时间：{{item.activityEndTime}}到期</text>
						</view>
					</view>
					<!-- 已完成 -->
					<view class="btns" v-if="item.orderStatus==3 ">
						<view @tap.stop="deleteOrder(item)" class="btn_blue">删除订单</view>
						<view v-if="item.evaluateFlag==0" @tap.stop="changeTicket(item)" class="btn_blue">评价</view>
						<view @tap.stop="zaici(item)" class="btn_blue">再来一单</view>
					</view>
					<!-- 待支付 -->
					<view class="btns" v-if="item.orderStatus==0">
						<view @tap.stop="cancelOrder(item)">取消订单</view>
						<view @tap.stop="toPay(item)" class="btn_blue"
							style="color: #FFFFFF; background-color: #FF4141;">去支付</view>
					</view>
					<!-- 待使用 -->
					<view class="btns" v-if="item.orderStatus==4">
						<!-- <view @tap.stop="cancelOrder(item)" class="btn_blue">取消订单</view> -->
						<view @tap.stop="toBooking(item)" class="btn_blue">去使用</view>
					</view>
					<!-- 已取消/已退款 -->
					<view class="btns" v-if="item.orderStatus==5||item.orderStatus==-1||item.orderStatus==-3">
						<view @tap.stop="deleteOrder(item)" class="btn_blue">删除订单</view>
						<view @tap.stop="zaici(item)" class="btn_blue">再来一单</view>
					</view>
				</u-list-item>
			</u-list>
		</block>
		<block v-else>
			<NoData style="margin: 0 100%;" />
		</block>

		<!-- 删除弹框 -->
		<u-modal :show="showdelete" :title="title1" :content='contented' showCancelButton="true" confirmText='确定'
			confirmColor='#007A69' @confirm="confirmed" @cancel='cancel'></u-modal>

	</view>
</template>
<script>
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				mysearch: '',
				orderList: [],
				oscConfig: {
					0: 'textColor000',
					4: 'textColor333'
				},
				pageIndex: 1,
				pageCount: 10,
				// isOver: false,
				FishJialeOcfg: this.$FishJialeOcfg,
				state: '',
				showdelete: false,
				orderType: null,
				delId: null,
				title1: '温馨提示',
				contented: '您确定要删除吗？确定删除将不可撤回！',
			}
		},
		methods: {
			// 取消
			cancel() {
				this.showdelete = false
			},
			deleteOrder(item) {
				this.showdelete = true
				this.delId = item.orderCode
			},


			// 确定删除
			confirmed() {
				let orderCode = this.delId
				this.$api.get('/api/merchant/food/logicDelOrder/' + orderCode).then((res) => {
					if (res.data.code == 0) {
						this.$showToast('删除成功');
						this.showdelete = false
						setTimeout(() => {
							this.init();
						}, 300)
					} else {
						this.$showModal(res.data.msg);
						this.showdelete = false
						setTimeout(() => {
							this.init();
						}, 300)
					}
				})
			},

			scrolltolower() {
				this.getList();
			},
			zaici(e) {
				uni.navigateTo({
					url: '/pagesTrip/fineFood/fineFood/orderforgoods?list=' + JSON.stringify({
						...e
					}) + '&cymsType=order'
				})
			},
			init() {
				this.pageIndex = 1;
				// this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			// 搜索
			search(keyword) {
				this.mysearch = keyword;
				this.pageIndex = 1;
				// this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			toBooking(item) {
				uni.navigateTo({
					url: '/pagesTrip/fineFood/fineFood/orderdetails?orderId=' + item.orderCode
				})
			},
			getList() {
				let that = this;
				this.$api.get('/api/merchant/food/queryPageUserOrder', {
					userId: JSON.parse(uni.getStorageSync('userInfo')).id,
					state: this.state,
					page: this.pageIndex,
					limit: this.pageCount,
					queryName: this.mysearch,
					merchantCategory: 4,

				}).then(res => {
					if (res.data.code === 0) {
						that.pageIndex++;
						let {
							list,
							total
						} = res.data.data;
						that.orderList = [...that.orderList, ...list];
						that.orderList.forEach(o => {
							if (o.orderStatus == 0) {
								let diffMS = Date.parse(new Date()) - Date.parse(new Date(o.createDate));
								if (diffMS < this.$gqTime) o.djsTime = this.$gqTime - diffMS -
									1000; //提前一秒，便于取消刷新
							}
						})
					} else that.$showToast(res.data.msg);
				})
			},
			changeStatus(index) {
				if (index === 0) this.state = '10';
				else if (index === 1) this.state = '0'
				else if (index === 2) this.state = '4';
				else if (index === 3) this.state = '11';
				else if (index === 4) this.state = '-3';
				this.init();
			},
			//评价
			changeTicket(item) {
				uni.navigateTo({
					url: '/pagesTrip/fineFood/fineFood/evaluate?list=' + JSON.stringify(item)
				})
			},
			//取消订单
			cancelOrder(item) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定取消订单？',
					success: function(res) {
						if (res.confirm) {
							that.$api.post('/api/merchant/food/scenic/merchant/order/cancelFoodOrder', {
								orderCode: item.orderCode,
							}).then(res => {
								if (res.data.code === 0) {
									that.$showToast('订单已取消');
									that.init();
								} else that.$showToast(res.data.msg)
							})

						}
					}
				});
			},
			// 删除订单
			del(item) {
				let ids = item.id;
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除订单？',
					success: function(res) {
						if (res.confirm) {
							that.$api.del('/api/fishermanOrder', [
								ids
							]).then(res => {
								if (res.data.code === 0) {
									that.$showToast('订单已删除');
									that.init();
								} else that.$showToast(res.data.msg)
							})
						}
					}
				});
			},
			//去支付
			toPay(item) {
				this.$api.post('/order/icbc/createOrder', {
					orderCode: item.orderCode,
					tradeType: 'JSAPI',
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId
				}, false).then(res => {
					if (res.statusCode == 200 && res.data != null) {
						if(res.data.appId){
							this.$wxPay(res.data).then(result => {
								this.$showToast('支付成功');
								this.init();
							})
						}else if(res.data.code && res.data.code!=0){
							this.$showModal(res.data.msg);
						}
					}else this.$showModal(res.data.msg);
				})
			},
			finish(item) {
				this.$api.post('/api/merchant/food/scenic/merchant/order/cancelFoodOrder', {
					orderCode: item.orderCode,
				}).then(res => {
					if (res.data.code === 0) {
						this.init();
					} else this.$showModal(res.data.msg)
				})
			}
		}
	}
</script>



<style scoped lang="less">
	.orders {
		display: flex;
		align-items: center;

		&.empty {
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		::v-deep .u-list-item {
			width: 100%;
			margin-top: 20rpx;
			border-radius: 16rpx;
			background: #fff;
			padding: 36rpx 24rpx 24rpx;
			box-sizing: border-box;

			&:last-child {
				margin-bottom: 40rpx;
			}

			.top {
				display: flex;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
						line-height: 28rpx;
						margin-left: 20rpx;
					}
				}

				.right {
					text-align: right;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					line-height: 37rpx;
				}
			}

			.content {
				padding: 22rpx 0 24rpx 56rpx;
				background: #fff;

				.title {
					display: flex;
					align-items: center;
					justify-content: space-between;

					text {
						font-size: 32rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #333333;
						line-height: 45rpx;

						&:last-child {
							color: #111111;
						}
					}
				}

				.date {
					display: flex;
					align-items: center;
					margin-top: 22rpx;

					&>text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #AAAAAA;
						line-height: 24rpx;
					}
				}

				.room_price {
					margin-top: 16rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #AAAAAA;
				}
			}

			.btns {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				border-top: 1rpx solid #EFEFEF;

				&>view {
					margin: 24rpx 0 0 24rpx;
					width: 148rpx;
					height: 56rpx;
					// border: 1rpx solid #EFEFEF;
					border-radius: 16rpx;
					line-height: 56rpx;
					text-align: center;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;

					&.btn_blue {
						// border: 1rpx solid #0DBFFD;
						background-color: #F6F6F6;
						// color: #0DBFFD;
					}
				}
			}

			&.qx {
				text {
					color: #999999 !important;
				}
			}
		}
	}

	.textColor999 {
		color: #999999;
	}

	.textColor333 {
		color: #333333;
	}

	.textColor000 {
		color: #FEB000;
	}
</style>